// pages/statis/statis.js
const util = require("../../util/util");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sum: [
      {title: "今日番茄次数", data: ""},
      {title: "累计番茄次数", data: ""},
      {title: "今日专注时长", data: ""},
      {title: "累计专注时长", data: ""}
    ],
    // 今日番茄
    todays: [],
    // 分类统计标题区域,默认显示今日
    currentTag: "today-list",
    // 分类统计显示列表
    cateList: [],
    // 没有数据
    noData: true
  },

  // 点击测试
  clickTest: function() {
    // {date: "2023-07-14 21:17:35", cate: "学习", time: "1分钟"}
    let logs = wx.getStorageSync('logs');
    for (let log of logs) {
      console.log(log.time.substring(0, log.time.indexOf("分钟")));
    }
  },

  // 改变 tags ，切换今日列表和累计列表
  changeTags: function(e) {
    if (e.currentTarget.dataset.tag === "history-list" && this.data.cateList.length > 0) {
      this.setData({
        noData: false
      });
    } else if (e.currentTarget.dataset.tag === "today-list" && this.data.todays.length > 0) {
      this.setData({
        noData: false
      });
    } else {
      this.setData({
        noData: true
      });
    }
    this.setData({
      currentTag: e.currentTarget.dataset.tag
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let logs = wx.getStorageSync('logs');
    // 今日专注时长
    let todayLong = 0;
    // 累计专注时长
    let totalLong = 0;
    let todayList = [];
    for (let log of logs) {
      if (util.isTody(log["date"])) {
        todayList.push(log);
        todayLong += parseInt(log.time.substring(0, log.time.indexOf("分钟")));
        this.setData({
          noData: false
        });
      }
      totalLong += parseInt(log.time.substring(0, log.time.indexOf("分钟")));
    }

    this.setData({
      "sum[0].data": todayList.length,
      "sum[1].data": logs.length,
      "sum[2].data": todayLong + "分钟",
      "sum[3].data": totalLong + "分钟",
      cateList: logs,
      todays: todayList
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    
  }
})